let { db } = require("../config/dbConnect");
const Model = require("../models/Model");

Array.prototype.remove = function (val) {
  let index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

module.exports = {
  //学科分类
  async getColNames(req, res) {
    let modelNames = db.modelNames();
    modelNames.remove("用户注册");
    res.send(modelNames);
  },
  //学习记录
  async getLearnRecord(req, res) {
    let data = await Model.learningRecord
      .find({})
      .catch((err) => console.log(err));
    res.send(data);
  },
  //按输入框value检索
  async getSearchValue(req, res) {
    let { searchValue } = req.query;
    let reg = new RegExp(searchValue, "i");
    let data = await Model.learningRecord
      .find({ courseName: { $regex: reg } })
      .catch((err) => console.log(err));
    console.log(data);
    res.send(data);
  },
  //记录每日学习记录
  updateTodos(req, res) {
    let { todos } = req.body;
    // todos = { date: new Date().toLocaleDateString(), ...todos };
    Model.learningRecord
      .insertMany(todos)
      .then((success) => {
        res.send({ data: success, status: 200 });
      })
      .catch((err) => {
        res.send({ data: err, status: 500 });
      });
  },
};

// {
//   date:2020-10-05T16:00:00.000+00:00,
//   up:"千锋Web前端学习营",
//   desc:"简明易懂",
//   courseName:"2020年 最新版 MongoDB数据库 基础入门到高级",
//   codePath :"D:IMYCODEWORKWorkspaceNodeExpressMongoDB定义model增加和查询数据以及生成接口文档",
//   isOver:true
// }
